package com.ty.bigevent.controller;

import com.ty.bigevent.model.dto.AddArticleRequest;
import com.ty.bigevent.model.dto.UpdateArticleRequest;
import com.ty.bigevent.model.entry.Article;
import com.ty.bigevent.service.ArticleService;
import com.ty.bigevent.utils.PageBean;
import com.ty.bigevent.utils.Result;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
@Slf4j
@AllArgsConstructor
public class articleController {
    private final ArticleService articleService;
//    @GetMapping("/list")
//    public String listArticle(){
//        return "listArticle";
//    }

    @PostMapping("")
    public Result addArticle(@RequestBody @Validated AddArticleRequest addArticleRequest){
        articleService.addArticle(addArticleRequest);
        return Result.success();
    }
    @PutMapping("")
    public Result updateArticle(@RequestBody @Validated UpdateArticleRequest updateArticleRequest){
        articleService.updateArticle(updateArticleRequest);
        return Result.success();
    }
    @GetMapping("")
    public Result<PageBean<Article>> listArticle(Integer pageNum, Integer pageSize,
                                                 @RequestParam(required = false) Integer categoryId,
                                                 @RequestParam(required = false) String state){
        PageBean<Article> pageBean = articleService.listArticle(pageNum, pageSize, categoryId, state);
        return Result.success(pageBean);

    }

    @GetMapping("/detail")
    public Result<Article> detailArticle(@RequestParam("id") Integer id){
        Article article = articleService.getArticleById(id);
        return Result.success(article);
    }

    @DeleteMapping("")
    public Result deleteArticle(@RequestParam("id") Integer id){
        articleService.removeArticleById(id);
        return Result.success();
    }
}
